package com.course.server.service;

import com.course.server.domain.CourseContentFile;
import com.course.server.domain.CourseContentFileExample;
import com.course.server.dto.CourseContentFileDto;
import com.course.server.dto.PageDto;
import com.course.server.mapper.CourseContentFileMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CourseContentFileService {

    @Resource
    private CourseContentFileMapper courseContentFileMapper;
    /**
     * 列表查询
     */
    public  List<CourseContentFileDto> list(String courseId) {

        CourseContentFileExample example = new CourseContentFileExample();
        CourseContentFileExample.Criteria criteria=example.createCriteria();
        criteria.andCourseIdEqualTo(courseId);
        List<CourseContentFile> fileList = courseContentFileMapper.selectByExample(example);
        return CopyUtil.copyList(fileList,CourseContentFileDto.class);
    }

    /**
     * 保存 id有更新的时候更新 无值的时候 新增
     * @param courseContentFileDto
     */
    public void save(CourseContentFileDto courseContentFileDto){
        CourseContentFile courseContentFile = CopyUtil.copy(courseContentFileDto,CourseContentFile.class);
        if(StringUtils.isEmpty(courseContentFileDto.getId())){
            this.insert(courseContentFile);
        }else{
            this.update(courseContentFile);
        }
    }

    /**
    * 更新
    */
    private void update(CourseContentFile courseContentFile) {
    courseContentFileMapper.updateByPrimaryKey(courseContentFile);
    }



    /**
     * 新增
     * @param courseContentFile
     */
    private void insert(CourseContentFile courseContentFile){
        courseContentFile.setId(UuidUtil.getShortUuid());
        courseContentFileMapper.insert(courseContentFile);
    }
        /**
         * 删除
         */
    public void delete(String id){
        courseContentFileMapper.deleteByPrimaryKey(id);
    }
}